//
//  OrderDetailTableViewCell.m
//  B2C
//
//  Created by 李小龙 on 2017/8/2.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "OrderDetailTableViewCell.h"

@implementation OrderDetailTableViewCell
#pragma mark - 订单详情
+ (instancetype)orderDetailCellWith:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath count:(NSInteger)count {
    NSString *identifier = @"";
    NSInteger index = 0;
    switch (indexPath.section) {
        case 0:
            switch (indexPath.row) {
                case 0:
                    identifier = @"cellFirst";
                    index = 0;
                    break;
                case 1:
                    identifier = @"cellSecond";
                    index = 1;
                    break;
                default:
                    break;
            }
            break;
        case 1:
        {
            if (indexPath.row == count) {
                identifier = @"cellFourth";
                index = 3;
            } else if (indexPath.row == count+1) {
                identifier = @"cellFifth";
                index = 4;
            } else {
                identifier = @"cellThird";
                index = 2;
            }
        } break;
        case 2:
            identifier = @"cellSixth";
            index = 5;
            break;
        default:
            break;
    }
    OrderDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"OrderDetailTableViewCell" owner:self options:nil] objectAtIndex:index];
        if (indexPath.section == 1 && indexPath.row == count) {
            [cell.fourthRefundButton setHalfCircleWithColor:[UIColor grayTextColor]];
        }
        
    }
    return cell;
}
- (void)updateCellWith:(OrderModel *)model indexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0:
            switch (indexPath.row) {
                case 0:
                {
                    if (model.isrefund) {
                        [self.firstRightLabel setText:@"退款申请"];
                    } else {
                        // 状态0已提交，1已付款，2已发货，3已签收，4已评价，5已取消，6已删除，7退款申请，8已退款
                        switch (model.omstatus.intValue) {
                            case 0:
                                [self.firstRightLabel setText:@"等待买家付款"];
                                break;
                            case 1:
                                [self.firstRightLabel setText:@"等待发货"];
                                break;
                            case 2:
                                [self.firstRightLabel setText:@"已发货"];
                                break;
                            case 3:
                                [self.firstRightLabel setText:@"已完成"];
                                break;
                            case 4:
                                [self.firstRightLabel setText:@"已评价"];
                                break;
                            case 5:
                                [self.firstRightLabel setText:@"已取消"];
                                break;
                            case 6:
                                [self.firstRightLabel setText:@"已删除"];
                                break;
                            case 7:
                                [self.firstRightLabel setText:@"退款申请"];
                                break;
                            case 8:
                                [self.firstRightLabel setText:@"已退款"];
                                break;
                            default:
                                break;
                        }
                    }
                } break;
                case 1:
                {
                    [self.secondNameLabel setText:[NSString stringWithFormat:@"收货人：%@",model.deliveryname]];
                    [self.secondPhoneLabel setText:[LXLIsPhoneNumber phoneNumber:model.deliverytel]];
                    [self.secondAddressLabel setText:[NSString stringWithFormat:@"%@ %@ %@ %@",model.pname,model.cname,model.aname,model.deliveryaddress]];
                    
                } break;
                default:
                    break;
            }
            break;
        case 1:
        {
            if (indexPath.row == model.orderson.count) {
                // 退款按钮
                [self.fourthRefundButton setTitle:@"退款" forState:UIControlStateNormal];
                switch (model.omstatus.integerValue) {
                    case 1: case 2: case 3:
                    {
                        if (!model.isrefund) {
                            [self.fourthLineView setHidden:NO];
                            [self.fourthRefundButton setHidden:NO];
                        } else {
                            [self.fourthLineView setHidden:YES];
                            [self.fourthRefundButton setHidden:YES];
                        }
                    } break;
                    default:
                    {
                        [self.fourthLineView setHidden:YES];
                        [self.fourthRefundButton setHidden:YES];
                    } break;
                }
            } else if (indexPath.row == model.orderson.count+1) {
                [self.fifthContentLabel setText:[NSString stringWithFormat:@"共计%@件商品，合计：",model.omnumber]];
                [self.fifthPriceLabel setText:[NSString stringWithFormat:@"¥%.2f",model.omprice.floatValue]];
            } else {
                OrdersonModel *goodModel = model.orderson[indexPath.row];
                
                [self.thirdImageView sd_setImageWithURL:GetUrl(goodModel.gimg) placeholderImage:PlaceholderImagePictures];
                [self.thirdNameLabel setText:goodModel.gname];
                [self.thirdCountLabel setText:[NSString stringWithFormat:@"x%@",goodModel.osnumber]];
                [self.thirdPriceLabel setText:[NSString stringWithFormat:@"¥%.2f",goodModel.osprice.floatValue]];
                // 属性
                NSString *specs = @"";
                for (NSInteger i = 0; i < goodModel.otops.count; i++) {
                    OtopsModel *otops = goodModel.otops[i];
                    if (i == 0) {
                        specs = otops.taname;
                    } else {
                        specs = [NSString stringWithFormat:@"%@;%@",specs,otops.taname];
                    }
                }
                [self.thirdSpecsLabel setText:specs];
            }
        } break;
        case 2:
            [self.sixthOrderNumberLabel setText:[NSString stringWithFormat:@"订单编号：%@",model.omid]];
            [self.sixthOrderDateLabel setText:[NSString stringWithFormat:@"订单日期：%@",[DateSomeThing getDateStrWithTimestampstr:model.createtime formatStr:@"yyyy-MM-dd HH:mm"]]];
            break;
        default:
            break;
    }
}
#pragma mark - 退款订单详情
+ (instancetype)refundDetailCellWith:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath count:(NSInteger)count {
    NSString *identifier = @"";
    NSInteger index = 0;
    switch (indexPath.section) {
        case 0:
        {
            identifier = @"cellFirst";
            index = 0;
        } break;
        case 1:
        {
            if (indexPath.row == count) {
                identifier = @"cellFourth";
                index = 3;
            } else if (indexPath.row == count+1) {
                identifier = @"cellSeventh";
                index = 6;
            } else {
                identifier = @"cellThird";
                index = 2;
            }
        } break;
        default:
            break;
    }
    OrderDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"OrderDetailTableViewCell" owner:self options:nil] objectAtIndex:index];
        if (indexPath.section == 1 && indexPath.row == count) {
            [cell.fourthRefundButton setHalfCircleWithColor:[UIColor grayTextColor]];
        }
        
    }
    return cell;
}
- (void)updateRefundDetailCellWith:(OrderModel *)model indexPath:(NSIndexPath *)indexPath count:(NSInteger)count {
    switch (indexPath.section) {
        case 0:
        {
            switch (indexPath.row) {
                case 0:
                {
                    [self.firstLeftLabel setText:@"退款状态"];
                    // 0已提交，1已退款，2退款失败，3退款驳回
                    switch (model.refund.firstObject.rstatus.intValue) {
                        case 0:
                            [self.firstRightLabel setText:@"退款中"];
                            break;
                        case 1:
                            [self.firstRightLabel setText:@"已退款"];
                            break;
                        case 2:
                            [self.firstRightLabel setText:@"退款失败"];
                            break;
                        case 3:
                            [self.firstRightLabel setText:@"退款驳回"];
                            break;
                        default:
                            break;
                    }
                } break;
                case 1:
                {
                    [self.firstLeftLabel setText:@"退款金额"];
                    [self.firstRightLabel setText:[NSString stringWithFormat:@"¥%.2f",model.refund.firstObject.rprice.floatValue]];
                } break;
                default:
                    break;
            }
            
        } break;
        case 1:
        {
            if (indexPath.row == count) {
                // 取消退款
                if ([model.refund.firstObject.rstatus isEqualToString:@"0"]) {
                    [self.fourthRefundButton setTitle:@"取消退款" forState:UIControlStateNormal];
                    [self.fourthRefundButton setEnabled:YES];
                } else {
                    [self.fourthRefundButton setTitle:@"已完成" forState:UIControlStateDisabled];
                    [self.fourthRefundButton setEnabled:NO];
                }
            } else if (indexPath.row == count+1) {
                [self.seventhReasonLabel setText:[NSString stringWithFormat:@"退款原因：%@",model.refund.firstObject.rcontent]];
                [self.seventhPriceLabel setText:[NSString stringWithFormat:@"退款金额：¥%.2f",model.refund.firstObject.rprice.floatValue]];
                [self.seventhDateLabel setText:[NSString stringWithFormat:@"申请日期：%@",[DateSomeThing getDateStrWithTimestampstr:model.refund.firstObject.createtime formatStr:@"yyyy-MM-dd HH:mm"]]];
            } else {
                OrdersonModel *goodModel = model.orderson[indexPath.row];
                
                [self.thirdImageView sd_setImageWithURL:GetUrl(goodModel.gimg) placeholderImage:PlaceholderImagePictures];
                [self.thirdNameLabel setText:goodModel.gname];
                [self.thirdCountLabel setText:[NSString stringWithFormat:@"x%@",goodModel.osnumber]];
                [self.thirdPriceLabel setText:[NSString stringWithFormat:@"¥%.2f",goodModel.osprice.floatValue]];
                // 属性
                NSString *specs = @"";
                for (NSInteger i = 0; i < goodModel.otops.count; i++) {
                    OtopsModel *otops = goodModel.otops[i];
                    if (i == 0) {
                        specs = otops.taname;
                    } else {
                        specs = [NSString stringWithFormat:@"%@;%@",specs,otops.taname];
                    }
                }
                [self.thirdSpecsLabel setText:specs];
            }
        } break;
        default:
            break;
    }
}
#pragma mark - IBAction
- (IBAction)refundButtonAction:(id)sender {
    if (self.detailBlock) {
        self.detailBlock(DetailCellClickTypeRefund);
    }
}
- (IBAction)copyButtonAction:(id)sender {
    if (self.detailBlock) {
        self.detailBlock(DetailCellClickTypeCopy);
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
